package com.penghaisoft.microwms.base.web.auth.controller;

import com.penghaisoft.microwms.base.web.auth.service.IMdTeamService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdTeam;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/team")
@Slf4j
public class MdTeamController extends BaseController {
	
		@Autowired
		private IMdTeamService mdTeamService;

	  /**
	   * 新增记录
	   * @param mdTeam
	   * @return
	   */
		@PostMapping("c")
		public Resp create(MdTeam mdTeam) {
			Resp resp = new Resp();
			mdTeam.preInsert();
			mdTeam.setActiveFlag("1");
			mdTeam.setCreateBy(getLoginName());
			mdTeam.setLastModifiedBy(getLoginName());
			// 判断班组名称 或 班组代码 是否重复
			Boolean flag = mdTeamService.findIt(mdTeam);
			if (!flag){
				resp.setCode(Constant.FAIL);
				resp.setMsg("班组名称或班组编码重复！");
				return resp;
			}
			resp =  mdTeamService.create(mdTeam);
			resp.setMsg("添加成功");
			return resp;
		}

	  /**
	   * 删除记录（逻辑删除active_flag=0)
	   * @param mdTeam
	   * @return
	   */
		@PostMapping("d")
		public TransResult delete(MdTeam mdTeam) {
			mdTeam.preUpdate();
			Resp resp =  mdTeamService.delete(mdTeam);
			return fromResp(resp);
		}

	  /**
	   * 查询列表
	   * @param
	   * @return
	   */
		@PostMapping("list")
		public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows,MdTeam mdTeam) {
			Pager<MdTeam> result = mdTeamService.findListByCondition(page,rows,mdTeam);
			return success(result);
		}

		/**
		 * 查询列表
		 * @param
		 * @return
		 */
		@GetMapping("getTeams")
		public TransResult getTeams() {
			MdTeam mdTeam = new MdTeam();
			mdTeam.setActiveFlag("1");
			List<MdTeam> list = mdTeamService.getTeams(mdTeam);
			return success(list);
		}

	  /**
	   * 查询单条
	   * @param id
	   * @return
	   */
		@GetMapping("r/{id}")
		public TransResult queryById(@PathVariable String id) {
			MdTeam  mdTeam = mdTeamService.findById(id);
			return success(mdTeam);
		}

	  /**
	   * 修改记录
	   * @param mdTeam
	   * @return
	   */
		@PostMapping("u")
		public TransResult update(MdTeam mdTeam) {
			mdTeam.preUpdate();
			mdTeam.setLastModifiedBy(getLoginName());
			Resp resp =  mdTeamService.update(mdTeam);
	    	return fromResp(resp);
		}

}
